//#include <unistd.h>  // for F_ok
#include <glib.h>
#include <stdio.h> 
#include <unistd.h>

#include "config.h"
#include "sys_log.h"

static void* set_running_config()
{
	char base[200] = {0};
    char path[200] = {0};
	FILE *fp;

	getcwd(base, 200); //get path
	
	/*add lib path in bashrc*/
	sprintf(path,"echo \"export LD_LIBRARY_PATH=%s:\"$\"LD_LIBRARY_PATH\" >> ~/.bashrc",base);

	fp = popen(path, "r");
	pclose(fp);

	/*add auto run in rc*/ 
	sprintf(path,"echo \"cd %s && ./webprint_keeper\" >> ~/.bashrc",base);

	fp = popen(path, "r");
	pclose(fp);
}



static void* test_set_running_config()
{
	  char base[200] = {0};
    char path[200] = {0};
	FILE *fp;
	int fd[2];
	pipe(fd);

	int pid=0;
	pid=fork();
	if( pid < 0 ){
	}else if( pid == 0 ) {

		dup2( fd[1], 1 );

		getcwd(base, 200); //get path
		
		/*add lib path in bashrc*/
		sprintf(path,"echo \"export LD_LIBRARY_PATH=%s:\"$\"LD_LIBRARY_PATH\" >> ~/.bashrc",base);
		printf("set path[%s]\n",path);

		fp = popen(path, "r");
		pclose(fp);

		/*add auto run in rc*/

		sleep(3);
		sprintf(path,"echo samsung|sudo echo %s/webprint_keeper & >> /etc/rc.local",base);
		printf("set auto[%s]\n",path);
		system(path);
		fflush(stdout);


		//system("samsung");
		//fp = popen(path, "r");
		//fp = popen("samsung", "r");
		//pclose(fp);
		
	}else {
		dup2( fd[0], 0 );
		system("samsung");
	}
}


int main(int argc,char *argv[])  
{  
	set_running_config();
//	test_set_running_config();
    return 0;  
}
//signal(SIGCHLD,SIG_IGN);  //let child to free itself
